package com.mall.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.context.annotation.Bean;

import java.io.FileInputStream;
import java.io.FileNotFoundException;


public class UpdateUtils {

    public static String updateFile(String path,String name) throws FileNotFoundException {
            String endpoint = "https://oss-cn-beijing.aliyuncs.com";
            String accessKeyId = "LTAI5tFGFHEZTPnxdzzYYDB2";
            String accessKeySecret = "NbEQxUDg3lRm6KVJf5xUZ7Bw3hMUbm";
            String bucketName = "xymall-zzz";

            String objectName = name;

            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("image/jpg");

            FileInputStream inputStream = new FileInputStream(path);
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            try {
                ossClient.putObject(bucketName, objectName, inputStream,metadata);
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }

            return "https://xymall-zzz.oss-cn-beijing.aliyuncs.com/" + name;
        }
    }














